/*
 * Created by JFormDesigner on Mon Dec 13 17:55:22 CST 2021
 */

package com.cn.项目.飞机预约管理系统.Maininterface;

import com.cn.项目.utils.JdbcUtils;

import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.*;


public class DeleteUsers extends JPanel {
    public DeleteUsers() {
        initComponents();
    }

    //删除
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        Connection conn = null;
        PreparedStatement st =null;
        ResultSet rs = null;
        String id = textField1.getText();
        String name = textField2.getText();
        try {
           conn = JdbcUtils.getConnection();

            String sql = "delete from users where id=? and uname=?";
            st  = conn.prepareStatement(sql);
            st.setString(1,id);
            st.setString(2,name);

            int i = st.executeUpdate();
            if (i>0){
                new Success();
            }else {
                new Fail();
            }

        } catch (SQLException e1) {
            e1.printStackTrace();
        }finally {
            JdbcUtils.release(conn,st ,rs);
        }

    }

    //重置
    private void button2ActionPerformed(ActionEvent e) {
        // TODO add your code here
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        label3 = new JLabel();
        textField2 = new JTextField();
        button1 = new JButton();
        button2 = new JButton();

        //======== this ========
        setLayout(null);

        //---- label1 ----
        label1.setText("删除用户");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 10f));
        label1.setForeground(Color.red);
        add(label1);
        label1.setBounds(130, 20, 180, 45);

        //---- label2 ----
        label2.setText("账号");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 6f));
        add(label2);
        label2.setBounds(30, 85, 75, 40);

        //---- textField1 ----
        textField1.setText("请输入要删除的账号");
        add(textField1);
        textField1.setBounds(125, 85, 200, 40);

        //---- label3 ----
        label3.setText("姓名");
        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 6f));
        add(label3);
        label3.setBounds(30, 165, 60, 30);

        //---- textField2 ----
        textField2.setText("请输入要删除的人的姓名");
        add(textField2);
        textField2.setBounds(125, 160, 200, 40);

        //---- button1 ----
        button1.setText("删除");
        button1.setFont(button1.getFont().deriveFont(button1.getFont().getSize() + 6f));
        button1.setForeground(new Color(255, 51, 51));
        button1.addActionListener(e -> button1ActionPerformed(e));
        add(button1);
        button1.setBounds(new Rectangle(new Point(55, 240), button1.getPreferredSize()));

        //---- button2 ----
        button2.setText("重置");
        button2.setFont(button2.getFont().deriveFont(button2.getFont().getSize() + 6f));
        button2.setForeground(new Color(0, 204, 0));
        button2.addActionListener(e -> button2ActionPerformed(e));
        add(button2);
        button2.setBounds(new Rectangle(new Point(200, 240), button2.getPreferredSize()));

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        textField2.setText(" ");
        textField1.setText(" ");
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JLabel label3;
    private JTextField textField2;
    private JButton button1;
    private JButton button2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
